home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / telecomm / sticpsrc.lzh / SOURCE.ARC / AUDIT.C < prev    next >
C/C++ Source or Header  |  1989-07-24  |  3KB  |  110 lines

  1. #include <stdio.h>
  2. #include "global.h"
  3. #include "mbuf.h"
  4.  
  5. union header {
  6.     struct {
  7.         union header *ptr;
  8.         unsigned size;
  9.     } s;
  10.     long l;
  11. };
  12. /* Perform sanity checks on mbuf. Print any errors, return 0 if none,
  13.  * nonzero otherwise
  14.  */
  15. audit(bp,file,line)
  16. struct mbuf *bp;
  17. char *file;
  18. int line;
  19. {
  20.     register struct mbuf *bp1;
  21.  
  22.     for(bp1 = bp;bp1 != NULLBUF; bp1 = bp1->next)
  23.         audit_mbuf(bp1,file,line);
  24. }
  25. audit_mbuf(bp,file,line)
  26. register struct mbuf *bp;
  27. char *file;
  28. int line;
  29. {
  30.     union header *blk;
  31.     char *bufstart,*bufend;
  32.     int16 overhead = sizeof(union header) + sizeof(struct mbuf);
  33.     int16 datasize;
  34.     int errors = 0;
  35.     char *heapbot,*heaptop;
  36.     extern char _Uend;
  37.     extern int _STKRED;
  38.  
  39.     if(bp == NULLBUF)
  40.         return;
  41.  
  42.     heapbot = &_Uend;
  43.     heaptop = (char *) -_STKRED;
  44.  
  45.     /* Does buffer appear to be a valid malloc'ed block? */
  46.     blk = ((union header *)bp) - 1;
  47.     if(blk->s.ptr != blk){
  48.         printf("Garbage bp %lx\n",ptr2long(bp));
  49.         errors++;
  50.     }
  51.     if((datasize = blk->s.size*sizeof(union header) - overhead) != 0){
  52.         /* mbuf has data area associated with it, verify that
  53.          * pointers are within it
  54.          */
  55.         bufstart = (char *)(bp + 1);
  56.         bufend = (char *)bufstart + datasize;
  57.         if(bp->data < bufstart){
  58.             printf("Data pointer before buffer\n");
  59.             errors++;
  60.         }
  61.         if(bp->data + bp->cnt > bufend){
  62.             printf("Data pointer + count past bounds\n");
  63.             errors++;
  64.         }
  65.     } else {
  66.         /* Dup'ed mbuf, at least check that pointers are within
  67.          * heap area
  68.         */
  69.  
  70.         if(bp->data < heapbot
  71.          || bp->data + bp->cnt > heaptop){
  72.             printf("Data outside heap\n");
  73.             errors++;
  74.         }
  75.     }
  76.     /* Now check link list pointers */
  77.     if(bp->next != NULLBUF && ((bp->next < (struct mbuf *)heapbot)
  78.          || bp->next > (struct mbuf *)heaptop)){
  79.             printf("next pointer out of limits\n");
  80.             errors++;
  81.     }
  82.     if(bp->anext != NULLBUF && ((bp->anext < (struct mbuf *)heapbot)
  83.          || bp->anext > (struct mbuf *)heaptop)){
  84.             printf("anext pointer out of limits\n");
  85.             errors++;
  86.     }
  87.     if(errors != 0){
  88.         dumpbuf(bp);
  89.         printf("PANIC: buffer audit failure in %s line %d\n",file,line);
  90.         fflush(stdout);
  91.         for(;;)
  92.             ;
  93.     }
  94.     return;
  95. }
  96. dumpbuf(bp)
  97. struct mbuf *bp;
  98. {
  99.     union header *blk;
  100.     if(bp == NULLBUF){
  101.         printf("NULL BUFFER\n");
  102.         return;
  103.     }
  104.     blk = ((union header *)bp) - 1;
  105.     printf("bp %lx tot siz %u data %lx cnt %u next %lx anext %lx\n",
  106.         ptr2long(bp),blk->s.size * sizeof(union header),
  107.         ptr2long(bp->data),bp->cnt,
  108.         ptr2long(bp->next),ptr2long(bp->anext));
  109. }
  110.